package com.mfcxy.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.ws.RequestWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mfcxy.entity.Permissions;
import com.mfcxy.exception.CustomException;
import com.mfcxy.service.IPermissionsService;
import com.mfcxy.tools.BackJson;

@Controller
@RequestMapping("/admin/permissions/")
public class PermissionsController {
	@Autowired
	private IPermissionsService prmissionsService;
	/**
	 * 权限视图
	 * @return
	 */
	@RequestMapping("list")
	public String list() {
		return "admin/permissions_list";
	}
	
	/**
	 * 查询
	 * @param pid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("findbypid")
	public BackJson findByPid(@RequestParam(name = "id" , defaultValue = "0") int id) {
		List<Permissions> list = prmissionsService.selectList(id);
		Map<String , Object> map = new HashMap<>();
		map.put("list", list);
		BackJson bj = new BackJson();
		bj.setCode(1);
		bj.setData(map);
		return bj;
	}
	
	/**
	 * ajax 保存
	 * @param p
	 * @return
	 * @throws CustomException
	 */
	@ResponseBody
	@RequestMapping(value = "add" , method = RequestMethod.POST)
	public BackJson save(@ModelAttribute Permissions p) throws CustomException {
		p.checkSave();
		prmissionsService.save(p);
		BackJson bj = new BackJson();
		Map<String, Object> map = new HashMap<>();
		map.put("p", p);
		bj.setCode(1);
		bj.setData(map);
		return bj;
	}

	//更新名称
	@ResponseBody
	@RequestMapping(value = "update" , method = RequestMethod.POST)
	public BackJson updatePtype(@ModelAttribute Permissions p) {
		prmissionsService.update(p);
		BackJson bj = new BackJson();
		bj.setCode(1);
		return bj;
	}

	//删除
	@ResponseBody
	@RequestMapping(value = "/del")
	public BackJson delPtype(@RequestParam int id) {
		prmissionsService.del(id);
		BackJson bj = new BackJson();
		bj.setCode(1);
		return bj;
	}
}
